home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL12.TXT < prev    next >
Text File  |  1986-04-05  |  7KB  |  246 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 55
  2.  
  3.  
  4. TURBO-LESSON 12:   FUNCTION APPLICATION - ERROR DETECTION
  5.  
  6. OBJECTIVES - In this lesson, you will learn about:
  7.  
  8. 1.  Error detection
  9. 2.  Using a predefined function
  10. 3.  Writing your own function
  11.  
  12.  
  13. 1.  Error detection.
  14.  
  15. In the previous lesson,  you found that some input values caused 
  16. a problem.  The function used in PROG11 calculated the cube of a 
  17. number entered.  If the result was outside the range of valid 
  18. integers (for this version of Pascal), a run-time error 
  19. terminated the execution of the program - NOT A NICE EVENT!
  20.  
  21. If you write programs for others to use, you will have to deal 
  22. with the problem of ERRORS.
  23.  
  24. There are several approaches to error handling:
  25.  
  26. (1) Error detection before it happens - prevent the occurrence of 
  27. the error.
  28.  
  29. (2) Error detection when it happens - take corrective action.
  30.  
  31. (3) Ignore the error - let the program bomb!
  32.  
  33. The 3rd is not usually acceptable - but may be o.k. in the early 
  34. stages of program development since you, the programmer, can fix 
  35. the problem.  You may also find alternate ways to program for the 
  36. same result while avoiding the possibility of the error.
  37.  
  38. The 2nd, error detection when it happens, will be explored later.  
  39. Input/Output errors are typical examples of this class of errors.
  40.  
  41. In this lesson, you will find ways to detect a problem and 
  42. prevent its occurrence.
  43.  
  44. ##### DO:
  45.  
  46. In PROG12, examine FUNCTION Cube.
  47.  
  48. The function has been expanded to detect integers which are too 
  49. small or too large to produce a valid integer cube.  If a number 
  50. is entered which would cause an error, the result is set to 0 
  51. instead of the erroneous result.
  52. î
  53. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 56
  54.  
  55.  
  56. ##### DO:
  57.  
  58. Run PROG12 several times using the following values for input:
  59.  
  60. 3,  -3,  31, 32, -32, -33, 0
  61.  
  62. Were all the results as expected?  The inputs,32 and -33, would 
  63. produce cubes out of the range of valid integers, so these two 
  64. should have given results of 0.  
  65.  
  66. What about 0 as an input?  Did you get the correct result? 
  67.  
  68. Can you determine whether a result of 0 is valid  (0 input) or 
  69. invalid (input of < -32 or > 31)?
  70.  
  71. Later in this lesson you will write your own function to deal 
  72. with this problem!
  73.  
  74.  
  75.  
  76. 2.  Using a predefined function.
  77.  
  78. Pascal provides many functions and procedures which are already 
  79. defined.  
  80.  
  81. Some advantages of using predefined subprograms:
  82.  
  83. (1)  The subprogram is already debugged.
  84.  
  85. (2)  The subprogram doesn't take up room in your program.
  86.  
  87. (3)  You can spend your time on more interesting programming, 
  88. no need to "reinvent the wheel".  
  89.  
  90. To use a predefined function, you have to know:
  91.  
  92. (1)  The name of the function
  93.  
  94. (2)  What goes in  (what values do you provide as input?)
  95.  
  96. (3)  What comes out  (what result is associated with the function 
  97. name?) 
  98.  
  99. The absolute value function, ABS, can be used in PROG12 to 
  100. illustrate the use of a predefined function.
  101.  
  102. What goes in              What comes out
  103.  
  104.     3 --------> [ABS] --------> 3
  105.  
  106.    -5 --------> [ABS] --------> 5
  107.  
  108. The absolute value function provides a positive number of the 
  109. same magnitude as the positive or negative number input to the 
  110. function.  
  111. î
  112. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 57
  113.  
  114.  
  115. ##### DO: 
  116.  
  117. Add the following statement after the ReadLn(No) statement in 
  118. PROG12:
  119.  
  120.   WriteLn('Absolute value: ', ABS(No) );
  121.  
  122. Run the program several times with both positive and negative 
  123. numbers.
  124.  
  125. NOTE: THERE ARE OFTEN SEVERAL WAYS TO DO THE SAME THING IN 
  126.       PROGRAMMING.  YOU SHOULD BE LOOKING FOR WAYS TO DECIDE 
  127.       WHICH OF SEVERAL PROGRAMMING SOLUTIONS IS BETTER IN A 
  128.       GIVEN CASE.
  129.  
  130. The next two exercises demonstrate two ways to use ABS in the 
  131. error detection problem.  You should decide which of the two is 
  132. better.  (Maybe neither is better than the present form of 
  133. PROG12).
  134.  
  135. ##### DO:
  136.  
  137. Change the WriteLn with the reference to Cube to:
  138.  
  139. WriteLn('The cube is; ', Cube(ABS(No)) );
  140.  
  141. Test the program with various inputs to make sure the results are 
  142. the same as before.
  143.  
  144. Is the sign correct on all cubes (using both positive and 
  145. negative inputs)?
  146.  
  147. Try -32 as an input.  What was the result?   Why?
  148. î
  149. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 58
  150.  
  151.  
  152. Now, try another way to use the ABS function.
  153.  
  154. ##### DO:
  155.  
  156. Restore the WriteLn to its original form:
  157.  
  158. WriteLn('The cube is: ', Cube(No) );
  159.  
  160. Also change the first line of the IF statement in the FUNCTION 
  161. Cube to: 
  162.  
  163. IF ABS(Number) > 31
  164.      
  165. Test the program with several values.  
  166.  
  167. Did you get the same results as in the previous exercise?
  168.  
  169. Which way do YOU think is best?  (Maybe neither because of the 
  170. problem with -32?)
  171.  
  172. How would you decide whether to use the function, ABS, in the 
  173. main program or in the function, Cube?
  174.  
  175. Is the action accomplished by ABS of interest to you in getting 
  176. the cube of a number?  If not, it should probably be pushed out 
  177. of the main program and into the subprogram.
  178.  
  179.  
  180. 3.  Writing your own function.
  181.  
  182. Now, it's your turn.  Another approach to the error detection 
  183. problem uses a second function, which you are about to write!
  184.  
  185. Give the function the name:        Has_Valid_Cube
  186.  
  187. The function will have one input:  Number  of type Integer
  188.  
  189. The type of the function will be:  Boolean
  190.  
  191. What the function does:  
  192.  
  193. If Number would produce a valid cube, the function, 
  194. Has_Valid_Cube, will have the value, TRUE.  
  195.  
  196. If Number would produce an error, Has_Valid_Cube will have the 
  197. value, FALSE.  
  198. î 
  199. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 59 
  200.  
  201.  
  202. ##### DO:
  203.  
  204. Write the function, Has_Valid_Cube.  Place it before the main 
  205. program.  It can be either before or after the function, Cube.
  206.  
  207. Look at FUNCTION Cube if you need help with the form of the 
  208. function declaration or the IF statement needed.
  209.  
  210. ##### DO:
  211.  
  212. In the main program, replace the WriteLn which references Cube 
  213. with the following: 
  214.  
  215. IF Has_Valid_Cube (No)
  216.   THEN
  217.     WriteLn('The cube is: ', Cube(No) )
  218.   ELSE
  219.     BEGIN
  220.       WriteLn('The cube of ',No,' is outside the integer range');
  221.       WriteLn('in this version of Pascal.');
  222.     END;
  223.  
  224. Test the program with several positive and negative values and 
  225. values which would cause erroneous cubes.
  226.  
  227. (If you have trouble writing the function, PROG12A is available 
  228. as a sample.  Don't check PROG12A until you have given it a try 
  229. on your own!)
  230.  
  231. Are there any other improvements you want to make to PROG12?
  232.  
  233. FUNCTION Cube still checks for invalid inputs.  Is this still 
  234. necessary?
  235.  
  236. ##### DO:
  237.  
  238. Change FUNCTION Cube so that it does no error checking, just 
  239. calculates the cube of the number input.
  240.  
  241. Test the program with several values including 0.  
  242.  
  243. Note that there is no longer any ambiguity when the result is a 
  244. cube of 0.  
  245. î
  246.